import { CreateMeta, HoleMeta } from '@/model/meta/MetaClass';
import AluGeoUtil from '@/model/geometry/utils/AluGeoUtil';
import AModelFmulaUtils from '@/model/utils/AModelFmulaUtils';
import { AK } from '@/model/const/ModelConst';

/** 角铝网格*/
export default class ALAluEntity {

    private _type: number = 0;

    geometryMeta: GeometryReturnMeta<Array<any>>;

    constructor(type: number) {
        this.geometryMeta = {
            vertices: [],
            indices: [],
            meshface: []
        };

        this._type = type;

        // this.create(data, type);
    }

    /**
     * 
     * @param r 
     * @param type 0 普通 1底角铝 2斜角铝
     * @returns 
     */
    create(data: AIBasicParams) {

        let vec = [];
        let indices = [];

        let L =  isNaN(data.L) ? 0 : +data.L;

        let ts = [data.ZYKH, data.SXKH, data.bkT];
        let rs = [-90, -90, -1 * (+data.angle)];
        let hs = [AModelFmulaUtils.getHolesbyExpress(data[AK.p_zkkw], L, 2, data, "p_zkkw"), AModelFmulaUtils.getHolesbyExpress(data[AK.p_skkw], L, 2, data, "p_skkw"), AModelFmulaUtils.getHolesbyExpress(data[AK.p_zkkw], L, 2, data, "p_zkkw")];
        let hs0 = [AModelFmulaUtils.getHolesbyExpress(data[AK.p_ykkw], L, 2, data, "p_ykkw"), [], AModelFmulaUtils.getHolesbyExpress(data[AK.p_ykkw], L, 2, data, "p_ykkw")];
        let ws = [parseFloat(data.W), parseFloat(data.W), parseFloat(data.W)];
        let ws0 = [parseFloat(data.H), parseFloat(data.H), parseFloat(data.H)];
        let ldis = [data[AK.p_zykbj], data[AK.p_kbj], data[AK.p_zykbj]];
        let rdis = [data[AK.p_yykbj], data[AK.p_kbj], data[AK.p_yykbj]];

        //右翼
        let bb = this.basicBoard(data, ws0[this._type], rs[this._type], ts[this._type], hs0[this._type], rdis[this._type]);
        //左翼
        let cc = this.activeBoard(data, ws[this._type], rs[this._type], ts[this._type], hs[this._type], ldis[this._type]);


        vec.push(...bb.vertices, ...cc.vertices);
        indices.push(...bb.indices, ...cc.indices.map((value) => { value += bb.vertices.length / 3; return value; }))

        if (this._type == 1) {
            let r = -0.5 * Math.PI;
            let cr = Math.cos(r);
            let sr = Math.sin(r);

            for (let i: number = 0; i < vec.length; i += 3) {
                let x = vec[i];
                let y = vec[i + 1];

                vec[i] = x * cr - sr * y;
                vec[i + 1] = sr * x + cr * y;
            }
        }

        this.geometryMeta.vertices = vec;
        this.geometryMeta.indices = indices;

        // this.setDuan(data);
        return {vertices : this.geometryMeta.vertices, indices : this.geometryMeta.indices};
    }


    /** 创建一个基准面
     * hr 边框孔直径
     * hx 孔的距离边框的大小
    */
    private basicBoard(data: AIBasicParams, w: number, r = -60, t: number = 0, hole = [], bj: number = 0): GeometryReturnMeta<Array<any>> {

        //取参数
        let l = parseFloat(data.L);

        let ra = r / 180 * Math.PI;
        let tr = Math.abs(Math.tan(ra / 2));

        let ww = tr == 0 ? 0 : t / tr;

        let back = [
            0, 0, 0, w, 0, 0, w, l, 0, 0, l, 0,
        ];

        let front = [
            ww, 0, t, w, 0, t, w, l, t, ww, l, t,
        ];


        let h = new HoleMeta(data.BKDia * 0.5, w - bj, hole);
        let c = new CreateMeta(t, front, back, h);
        return AluGeoUtil.createBoardWithHole(c);
    }

    /** 创建一个活动板(旋转等)*/
    private activeBoard(data: AIBasicParams, w: number, r = -60, t: number = 0, hole = [], bj: number = 0): GeometryReturnMeta<Array<any>> {
        let l = parseFloat(data.L);


        let rr = r / 180 * Math.PI;
        let cr = Math.cos(rr);
        let sr = Math.sin(rr);
        let tr = Math.abs(Math.tan(rr / 2));

        let ww = tr == 0 ? 0 : t / tr

        let back = [
            ww, 0, 0, w, 0, 0, w, l, 0, ww, l, 0,
        ];
        let front = [
            0, 0, t, w, 0, t, w, l, t, 0, l, t,
        ];

        let h = new HoleMeta(data.BKDia * 0.5, w - bj, hole);
        let c = new CreateMeta(t, front, back, h);
        let result = AluGeoUtil.createBoardWithHole(c);


        for (let i: number = 2; i < result.vertices.length; i += 3) {
            result.vertices[i] -= t;
        }


        for (let i: number = 0; i < result.vertices.length; i += 3) {
            let x = result.vertices[i];
            let z = result.vertices[i + 2];

            result.vertices[i] = x * cr + sr * z;
            result.vertices[i + 2] = cr * z - x * sr;


        }

        return result;
    }
}